from rest_framework.generics import ListAPIView, CreateAPIView
from rest_framework.permissions import IsAdminUser
from meiduo_admin.my_paginations import MyPageNumberPagination
from meiduo_admin.users.serializers import UsersModelSerializer
from users.models import User


# 用户基本数据查询,分页
class UsersView(ListAPIView, CreateAPIView):

    serializer_class = UsersModelSerializer
    pagination_class = MyPageNumberPagination
    permission_classes = [IsAdminUser]
    queryset = User.objects.filter(is_staff=False, is_superuser=False)

    def get(self, request, *args, **kwargs):

        keyword = request.query_params.get('keyword')
        self.queryset = User.objects.filter(username__contains=keyword, is_staff=False, is_superuser=False)

        return self.list(request, *args, **kwargs)


